package com.uenpay.im.ui.old.activity.map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.qyx.android.weight.utils.baidumap.BaiDuMapUtil;
import com.qyx.android.weight.utils.baidumap.IGetLocationListener;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

/**
 * 查看地理位置
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2014/7/10
 *
 */
public class LocationNavigationActivity extends Activity implements
		OnGetGeoCoderResultListener {

	private double endLatitude = 0.0;
	private double endLongitude = 0.0;

	private TextView showAddr;
	// LocationClient mLocClient;
	BitmapDescriptor mCurrentMarker;
	MapView mMapView;
	BaiduMap mBaiduMap;

	GeoCoder mSearch = null;
	OnCheckedChangeListener radioButtonListener;
	boolean isFirstLoc = true;
	boolean is_exit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		SDKInitializer.initialize(this.getApplicationContext());
		setContentView(R.layout.activity_location_navigation);
		showAddr = (TextView) findViewById(R.id.map_bubbleText);
		TextView title = (TextView) findViewById(R.id.title_textview);
		title.setText(getResources().getString(R.string.look_location));
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);
		if (!TextUtils.isEmpty(getIntent().getStringExtra("mineLng"))
				&& !TextUtils.isEmpty(getIntent().getStringExtra("mineLat"))) {
			endLongitude = Double.parseDouble(getIntent().getStringExtra(
					"mineLng"));
			endLatitude = Double.parseDouble(getIntent().getStringExtra(
					"mineLat"));

			double[] end = BaiDuMapUtil.gaodeToBaidu(endLatitude, endLongitude);
			endLatitude = end[0];
			endLongitude = end[1];
			// Log.e("endLatitude",endLatitude+"");
			// Log.e("endLongitude",endLongitude+"");
		}

		mMapView = (MapView) findViewById(R.id.get_now_addresss_map_view);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMyLocationEnabled(true);
		float zoomLevel = 16;
		MapStatusUpdate u2 = MapStatusUpdateFactory.zoomTo(zoomLevel);
		mBaiduMap.animateMapStatus(u2);

		// mLocClient = new LocationClient(this);
		// mLocClient.registerLocationListener(myListener);
		// LocationClientOption option = new LocationClientOption();
		// option.setOpenGps(true);// ��gps
		// option.setCoorType("bd09ll");
		// option.setScanSpan(1000);
		// mLocClient.setLocOption(option);
		// mLocClient.start();
		getLocation();

		findViewById(R.id.back_layout).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						back();
					}
				});
	}

	private void getLocation() {
		IMApplication.getInstance().GetBaiduLoacationUtil()
				.getLoaction(new IGetLocationListener() {

					@Override
					public void onSuccess(BDLocation location) {
						if (is_exit || isFinishing()) {
							return;
						}
						MyLocationData locData = new MyLocationData.Builder()
								.latitude(location.getLatitude())
								.longitude(location.getLongitude()).build();
						mBaiduMap.setMyLocationData(locData);

						// startLatitude = location.getLatitude();
						// startLongitude = location.getLongitude();

						Message msg = myHandler.obtainMessage();
						msg.what = 0;
						myHandler.sendMessage(msg);
					}

					@Override
					public void onFail() {
					}
				});
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mBaiduMap != null) {
			mBaiduMap.setMyLocationEnabled(false);
		}
		if (mMapView != null) {
			mMapView.onDestroy();
			mMapView = null;
		}
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		back();
		super.onBackPressed();
	}

	private Handler myHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {

			if (msg.what == 0) {
				SearchButtonProcess();
			}

			return false;
		}
	});

	@Override
	public void onGetGeoCodeResult(GeoCodeResult result) {

	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		if (isFirstLoc) {
			mBaiduMap.addOverlay(new MarkerOptions().position(
					result.getLocation()).icon(
					BitmapDescriptorFactory
							.fromResource(R.drawable.map_point_navigation)));

			if (!TextUtils.isEmpty(result.getAddress())) {
				showAddr.setText(result.getAddress());
			}

			isFirstLoc = false;
			LatLng ll = new LatLng(endLatitude, endLongitude);
			MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
			mBaiduMap.animateMapStatus(u);
		}
	}

	/**
	 *
	 */
	public void SearchButtonProcess() {
		LatLng ptCenter = new LatLng(endLatitude, endLongitude);
		mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
	}

	private void back() {
		is_exit = true;
		finish();
	}
}
